$(document).ready(function() {
	
	initMap();
	getDataToMapMarker(0);
	
});
$(function(){
	var i = 0;
	$(".navigationImage").each(function(index){
			$(this).mouseover(function(){
				if(i != index){
					getDataToMapMarker(index);
					doClosePromptContext();
					i = index;
				}
				});
		});	
});
var mapObj;
var userDefaultPosition;
var userPositionMarker;

// 初始化地图

function initMap() {
	
	userDefaultPosition = new AMap.LngLat(103.98757, 30.580274);
	
	mapObj = new AMap.Map("map_container", {
    	level: 5, //地图初始化时的视野级别
    	center: userDefaultPosition, //地图初始化时的中心点经纬度坐标
    	zooms : [3,18], //地图视野级别范围，如：[3,19]，3 为最小级别，19 为最大级别
    	dragEnable : true, //定义地图是否可以拖拽
    	zoomEnable : true, //定义地图视野级别是否可以更改
    	keyboardEnable : true, //定义地图是否可以被键盘控制
    	jogEnable: true, //定义地图拖拽时是否支持缓动效果
    	continuousZoomEnable: true, //定义地图是否有连续缩放效果
    	doubleClickZoom : false, //定义地图是否可以双击放大
    	scrollWheel: true, //定义地图是否支持鼠标滚轮缩放
    	cursor: 'auto'//鼠标移上去以后显示为指针
    });
}
function getDataToMapMarker(index){
	
	$.ajax({
		url: "mapMarkerData",
		data: {
			'clickNumber':index
		},
		type: 'post',
		async: true,
		dataType: 'json',
		success: function(resultData){
			var resultListSize = resultData.list.length;
			for(var i = 0; i < resultListSize ; i++){
					addMarker(i,resultData.list[i]);
				}
			}
		});
}
function addMarker(i,data) {
	var markerOption = {
			icon:"http://api.amap.com/webapi/static/Images/" + (i + 1) + ".png",
			position:new  AMap.LngLat(data.x,data.y),
			offset:{x:-23,y:-46}
			};
	var marker = new AMap.Marker(markerOption);
		marker.id = i + 1;
		mapObj.addOverlays(marker);
		mapObj.bind(marker,"click",function(e){
			setMapCenterAndPromptMarker(e.lnglat,data);
			});
}
function setMapCenterAndPromptMarker(lngLat,data){
	
	var promptContext = [];	
		promptContext.push("<div  id='promptContextId' style='width: 250px;height: 120px;border: 1px solid #E8E8E8;background-color: white;dispaly:block;position: absolute;z-index: 1000;'>");
		promptContext.push("<div style='width: 250px;height: 36px;border-bottom: 1px solid #E8E8E8;'>");
		promptContext.push("<div style='float: left;font-weight: bold;width: 190px;height: 36px;line-height: 36px;margin-left: 15px;font-size: 15px;'>"+data.title+"</div>");
		promptContext.push("<div style='float: left;width: 36px;height:36px;margin-left: 5px;'>"+"<img onclick='doClosePromptContext()' src='../resource/images/pages/close.jpg'></div>");
		promptContext.push("<div style='clear: both;'></div>");
		promptContext.push("</div>");
		promptContext.push("<div style='width: 250px;height: 80px;margin-left: 25px;margin-top: 30px;font-size: 12px;'>");
		promptContext.push("<div style='margin-bottom :7px;'>"+data.address+"</div>");
		promptContext.push("<div>"+data.phoneNumber+"</div>");
		promptContext.push("</div>");
		promptContext.push("</div>");
		promptContext.push("<div class='promotMarker-buttom-pointer'></div>");
	promptMarker = new AMap.Marker({
		id: "promptContext",
		zIndex: 4,
		cursor: 'auto',
		position: lngLat,
		content: promptContext.join(""),
		offset: {x: -50, y: -180}
	});
	mapObj.addOverlays(promptMarker);
	mapObj.setCenter(new AMap.LngLat(lngLat.lng,lngLat.lat));
	mapObj.panBy(parseInt(-20),parseInt(50)); 
	//markerMove(lngLat.lng,lngLat.lat);
}
function removeMarker(marker){
	if (marker) {
		mapObj.removeOverlays(marker);
	}
}
/*function markerMove(px,py){   
	var pixel=mapObj.lnglatTocontainer(new AMap.LngLat(px,py),10);   
	var mapDivWidth = 480;
	var mapDivHeight = 300;
	var minX = 48;
	var minY = 179;
	var moveX = 0;
	var moveY = 0;
	var errandX = pixel.x - minX;
	var errandY = pixel.y - minY;
		alert(errandX+"==="+errandY);
		if(errandX < 0 && errandY < 0){
			moveX  = - errandX + 30;
			moveY  = - errandY + 30;
			mapObj.panBy(parseInt(moveX),parseInt(moveY));
		}
		if(0< errandX < 230 && errandY < 0){
			
			moveY  = - errandY + 30;
			mapObj.panBy(parseInt(moveX),parseInt(moveY));
		}
}   */

function doClosePromptContext(){
	$("#promptContextId").css("display","none");
	$(".promotMarker-buttom-pointer").css("display","none");
}